package redis;

import pub.tbc.toolkit.Objs;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author tbc on 2016/12/7 10:16:15.
 */
public class RedisHelper {
    private static JedisPool pool = null;

    private static String getRedisHost() {
        return "192.168.1.11";
    }

    private static int getRedisPort() {
        return 6379;
    }

    private static void initPool() {
        if (Objs.isEmpty(pool)) {
            synchronized (RedisHelper.class) {
                pool = new JedisPool(new JedisPoolConfig(), getRedisHost(), getRedisPort());
            }
        }
    }

    public static Jedis getClientFromPool() {
        if (Objs.isEmpty(pool)) {
            synchronized (RedisHelper.class) {
                if (Objs.isEmpty(pool)) pool = new JedisPool(new JedisPoolConfig(), getRedisHost(), getRedisPort());
            }
        }
        return pool.getResource();
    }

    public static Jedis createClient() {
        return new Jedis(getRedisHost(), getRedisPort());
    }
}
